package com.pakazaka.roguelike.ui;

import com.pakazaka.controllers.Controller;
import com.pakazaka.enums.ItemPropertyType;
import com.pakazaka.enums.MoveDirectionType;
import com.pakazaka.enums.PlayerStateType;
import com.pakazaka.player.Player;
import com.pakazaka.simplecrawler.Core;
import static com.pakazaka.simplecrawler.Core.getInstance;
import com.pakazaka.simplecrawler.SimpleCrawler;
import com.pakazaka.testItemsSystem.Item;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

/**
 *
 * @author Remi
 */
public class FSInventory {

    private final ArrayList<Item> items;
    private final ItemDescWindow descriptionWindow = new ItemDescWindow();
    private boolean paintDescriptionWindow, paintInventory;

    public boolean isPaintDescriptionWindow() {
        return paintDescriptionWindow;
    }

    public void setPaintDescriptionWindow(boolean paintDescriptionWindow) {
        this.paintDescriptionWindow = paintDescriptionWindow;
    }

    public boolean isPaint() {
        return paintInventory;
    }

    public void setPaint(boolean paintInventory) {
        this.paintInventory = paintInventory;
        if (!paintInventory) {
            paintDescriptionWindow = false;
        }
    }
    private MoveDirectionType direction = MoveDirectionType.UP;

    public FSInventory(ArrayList<Item> items) {
        this.items = items;
    }

    public void paint(Graphics g) {
        Player player = getInstance().getPlayer();

        if (paintDescriptionWindow) {
            descriptionWindow.paint(g);
        } else {
            g.setFont(new Font("Consolas", Font.BOLD, 16));
            g.setColor(Color.black);
            g.fillRect(0, 0, SimpleCrawler.windowWidth, SimpleCrawler.windowHeight);
            g.setColor(Color.white);
            g.drawString("INVENTORY:", 2, 12);

            g.drawString("Volume: " + player.getCarryVolume() + "/" + player.getMaxCarryVolume(), 150, 12);

            for (int i = 0; i < items.size(); i++) {
                g.drawString(items.get(i).getBind() + " " + items.get(i).getName(), 2, 48 + i * 16);
            }
        }

    }

    public ItemDescWindow getDescriptionWindow() {
        return descriptionWindow;
    }

    public boolean isShowItemDescription() {
        return paintDescriptionWindow;
    }

    //keys
    public void inventoryDescriptions(KeyEvent e, int x, int y, ArrayList<Item> item) throws UnsupportedEncodingException {
        Core core = getInstance();

        if (core.getPlayer().getState() == PlayerStateType.IN_INVENTORY) {
            //просмотр описаний (I)
            for (int i = 0; i < item.size(); i++) {
                if (e.getKeyChar() == item.get(i).getBind()) {
                    descriptionWindow.setItem(item.get(i));
                    paintDescriptionWindow = !paintDescriptionWindow;
                }
            }
        } else { //дроп вещи в направлении (Shift + D)
            if (core.getPlayer().getState() == PlayerStateType.DROP_AROUND) {
                for (int i = 0; i < item.size(); i++) {
                    if (e.getKeyChar() == item.get(i).getBind()) {
                        if (item.get(i).isPropertyExist(ItemPropertyType.CAN_DROP)) {
                            switchItemCoordinatesAndDropThen(item.get(i), direction, x, y);
                        } else {
                            core.getConsole().pushMessage("You cannot drop this.");
                            setPaint(false);
                            core.setWhereMessage("");
                        }
                        core.getPlayer().setState(PlayerStateType.NORMAL);
                    }
                }
            } else //обычный дроп под ноги
            if (core.getPlayer().getState() == PlayerStateType.DROP) {
                for (int i = 0; i < item.size(); i++) {
                    if (e.getKeyChar() == item.get(i).getBind()) {
                        if (item.get(i).isPropertyExist(ItemPropertyType.CAN_DROP)) {
                            switchItemCoordinatesAndDropThen(item.get(i), MoveDirectionType.UNDEFINED, x, y);
                        } else {
                            core.getConsole().pushMessage("You cannot drop this.");
                            setPaint(false);
                            core.setWhereMessage("");
                        }
                        core.getPlayer().setState(PlayerStateType.NORMAL);
                    }
                }
            }
        }
    }

    public void setDirection(MoveDirectionType direction) {
        this.direction = direction;
    }

    public void switchItemCoordinatesAndDropThen(Item item, MoveDirectionType direction, int x, int y) {
        Core core = getInstance();
        switch (direction) {
            case UP:
                y--;
                break;
            case DOWN:
                y++;
                break;
            case LEFT:
                x--;
                break;
            case RIGHT:
                x++;
                break;
            default:
                break;
        }
        core.getPlayer().dropItem(item, x, y);
        core.getConsole().pushMessage("You've dropped your " + item.getName().toLowerCase());
        core.getConsole().pushDebugMessage("dropped id: " + item.getId() + ", type: " + item.getTypeInType());
        setPaint(false);
        core.setWhereMessage("");
        Controller.setPressed(true);
    }
}
